package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.MomentSupportForm;
import com.springboot.cloud.demos.entity.param.MomentSupportQueryParam;
import com.springboot.cloud.demos.entity.po.Moment;
import com.springboot.cloud.demos.entity.po.MomentComment;
import com.springboot.cloud.demos.entity.po.MomentSupport;
import com.springboot.cloud.demos.producer.service.IMomentService;
import com.springboot.cloud.demos.producer.service.IMomentSupportService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/momentSupport")
@Api("momentSupport")
@Slf4j
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
public class MomentSupportController {

    @Autowired
    private IMomentSupportService momentSupportService;
    @Autowired
    private IMomentService momentService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "userMomentSupportForm", value = "新增产品form表单", required = true, dataType = "MomentSupportForm")
    @PostMapping
    public Result add(@Valid @RequestBody MomentSupportForm momentSupportForm) {
        log.info("name:{}", momentSupportForm);
        MomentSupport momentSupport = momentSupportForm.toPo(MomentSupport.class);
        MomentSupport momentSupport1 = momentSupportService.queryByUser(momentSupport.getUserId(), momentSupport.getType(), momentSupport.getMomentId());
       if(momentSupport1!=null){
           Result.success(momentSupport1);
       }
        //同时更新评论数
        Moment moment = momentService.get(momentSupportForm.getMomentId());
        if (momentSupport.getType() == 0) {
            moment.setSupportNum(moment.getSupportNum() + 1);

        } else {
            moment.setSignUpNum(moment.getSupportNum() + 1);
        }
        momentService.update(moment);
        momentSupportService.add(momentSupport);
        return Result.success(momentSupport);
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable (value="id")String id) {

        MomentSupport momentSupport = momentSupportService.get(id);
        //同时更新评论数
        Moment moment = momentService.get(momentSupport.getMomentId());
        if(momentSupport.getType()==0){
            moment.setSupportNum(moment.getSupportNum() - 1);
        }else {
            moment.setSignUpNum(moment.getSignUpNum() - 1);
        }

        momentService.update(moment);

        return Result.success(momentSupportService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userMomentSupportForm", value = "产品实体", required = true, dataType = "MomentSupportForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody MomentSupportForm userMomentSupportForm) {
        MomentSupport userMomentSupport = userMomentSupportForm.toPo(MomentSupport.class);
        userMomentSupport.setId(id);
        return Result.success(momentSupportService.update(userMomentSupport));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(momentSupportService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "momentId", value = "单元Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/queryByUser")
    public Result<MomentSupport> queryByUser(@RequestParam String userId, @RequestParam Integer type, @RequestParam String momentId) {
        return Result.success(momentSupportService.queryByUser(userId, type, momentId));
    }

    @ApiOperation(value = "查询报名用户列表", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "momentId", value = "单元Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/querySignUpList")
    public Result querySignUpList(@RequestParam String momentId) {
        return Result.success(momentSupportService.querySignUpList(momentId));
    }
}

